<template>
  <div>
    <div class="w-full">
      <Button class="m-r-5px" type="primary" @click="deleteNotUseDir" :loading="deleteNotUseDirLoading">删除不使用的附件文件夹</Button>
      <Button class="m-r-5px" type="primary" @click="clearTempDir" :loading="clearTempDirLoading">清空临时文件目录</Button>
      <Tooltip placement="top" content="刷新数据并返回顶级目录">
        <Button class="m-r-5px" type="primary" @click="loadAttachmentInfos()">
          <Icon type="md-refresh"/>
        </Button>
      </Tooltip>

      <div class="float-right">
        <Input v-model="searchFormData.fileName" class="m-r-5px" style="width: 250px;" placeholder="文件名称" @on-enter="loadAttachmentInfos"/>
        <Button class="m-r-5px" @click="loadAttachmentInfos()">
          <Icon type="md-search"/>
          搜索
        </Button>
        <Button @click="searchFormData.fileName = ''; loadAttachmentInfos()">清空</Button>
      </div>
    </div>

    <div class="m-b-10px"></div>
    <Table :columns="columns" :data="tableData" :loading="loading"></Table>

    <Divider :style="{marginTop: '0px', marginBottom: '10px'}"/>
    <Page size="small" class="float-right" :total="pageTotal" :current.sync="pageIndex" :page-size="pageSize"
          show-elevator show-total @on-change="loadAttachmentInfos()">
    </Page>
  </div>
</template>

<script>
import ManageMixin from "@/mixin/ManageMixin";

export default {
  name: "AttachmentManage",
  mixins: [ManageMixin],
  data() {
    return {
      loading: false,
      deleteNotUseDirLoading: false,
      clearTempDirLoading: false,
      pageSize: 15,
      searchFormData: {
        fileName: ''
      },
      columns: [
        /*{
          type: 'selection',
          width: 50,
          align: 'center'
        },*/
        {
          title: '类型',
          key: 'directory',
          align: 'center',
          width: 80,
          render: (h, params) => {
            if (params.row.directory) {
              return h('Tooltip', {
                props: {
                  content: '目录类型',
                  placement: 'left'
                }
              }, [
                h('Icon', {
                  props: {
                    type: 'md-folder',
                    size: 26
                  }
                })
              ])
            } else {
              return h('Tooltip', {
                props: {
                  content: '文件类型',
                  placement: 'left'
                }
              }, [
                h('Icon', {
                  props: {
                    type: 'md-document',
                    size: 26
                  }
                })
              ])
            }
          }
        },
        {
          title: '文件名称',
          key: 'fileName',
          width: 300,
          render: (h, params) => {
            if (params.row.directory) {
              return h('a', {
                style: {
                  color: '#fff',
                  'text-decoration': 'underline'
                },
                attrs: {
                  href: 'javascript:;'
                },
                on: {
                  click: () => {
                    this.loadAttachmentInfos(params.row.absolutePath)
                  }
                }
              }, params.row.fileName)
            }else {
              return h('span', {}, params.row.fileName)
            }
          }
        },
        {
          title: '绝对路径',
          key: 'absolutePath',
        },
        {
          title: '最后更新日期',
          key: 'lastModifiedFormatting',
          width: 170
        },
        // {
        //   title: '操作',
        //   key: 'action',
        //   width: 80,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Poptip', {
        //         props: {
        //           confirm: true,
        //           transfer: true,
        //           placement: 'top-end',
        //           title: '您确定要删除此项吗？'
        //         },
        //         style: {},
        //         on: {
        //           'on-ok': () => {
        //
        //           }
        //         }
        //       }, [
        //         h('Button', {
        //           props: {
        //             size: 'small',
        //             type: 'error',
        //             icon: 'md-trash'
        //           }
        //         },)
        //       ])
        //     ]);
        //   }
        // }
      ],
    }
  },
  mounted() {
    this.loadAttachmentInfos();
  },
  methods: {
    loadAttachmentInfos(dirPath) {
      this.loading = true
      this.$PnApi.AttachmentApi.getAttachmentInfoByPage(this.pageIndex, this.pageSize, this.searchFormData.fileName, dirPath).then(result => {
        this.tableData = result.data.payload.list;
        this.pageTotal = result.data.payload.total;
        this.loading = false
      })
    },

    deleteNotUseDir() {
      this.$Modal.confirm({
        title: '提醒',
        content: '此操作会将附件根目录中所有未关联页面或页面模板等系统数据的目录或文件删除，确定要执行吗？',
        onOk: () => {
          this.deleteNotUseDirLoading = true
          this.$PnApi.AttachmentApi.deleteNotUseDir().then(result => {
            if (result.data.success) {
              this.loadAttachmentInfos();
              this.$Message.success(result.data.message)
            }else {
              this.$Message.error(result.data.message)
            }
            this.deleteNotUseDirLoading = false
          })
        }
      });
    },

    clearTempDir() {
      this.$Modal.confirm({
        title: '提醒',
        content: '确定清空所有临时文件目录吗？',
        onOk: () => {
          this.clearTempDirLoading = true
          this.$PnApi.AttachmentApi.clearTempDir().then(result => {
            if (result.data.success) {
              this.loadAttachmentInfos();
              this.$Message.success(result.data.message)
            }else {
              this.$Message.error(result.data.message)
            }
            this.clearTempDirLoading = false
          })
        }
      });
    }
  },
  computed: {},
  watch: {}
}
</script>

<style scoped>

</style>
